//
//  HUDManager.m
//  yilingdoctorCRM
//
//  Created by zhangxi on 14/10/28.
//  Copyright (c) 2014年 yuntai. All rights reserved.
//

#import "HUDManager.h"

static MBProgressHUD *HUDView;
@implementation HUDManager

+ (void)showWarningWithText:(NSString *)text {
    [HUDManager hideHUDView];
//    UIWindow *window = [[UIApplication sharedApplication].windows objectAtIndex:0];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self rootView] animated:YES];
    hud.labelText = text;
    hud.mode = MBProgressHUDModeText;
    hud.dimBackground = NO;
    hud.margin = 12.f;
    [hud hide:YES afterDelay:1.0];
}

+ (void)showLoadingHUDView:(UIView*)view
{
    HUDView = [MBProgressHUD showHUDAddedTo:view animated:YES];
    HUDView.mode = MBProgressHUDModeIndeterminate;
    HUDView.margin = 18.f;
    HUDView.opacity = 0.55;
    HUDView.dimBackground = YES;
//    HUDView.labelText = @"";
    HUDView.minShowTime = 0.3;
    HUDView.labelFont = [UIFont boldSystemFontOfSize:14];
}

+ (void)showLoadingHUDView:(UIView*)view withText:(NSString *)text {
    HUDView = [MBProgressHUD showHUDAddedTo:view animated:YES];
    HUDView.mode = MBProgressHUDModeIndeterminate;
    HUDView.margin = 18.f;
    HUDView.opacity = 0.55;
    HUDView.dimBackground = YES;
    HUDView.labelText = text;
    HUDView.minShowTime = 0.3;
    HUDView.labelFont = [UIFont boldSystemFontOfSize:14];
}

+ (void)hideHUDView
{
    [HUDView hide:YES];
}

+ (void)showLoadingHUDViewWithText:(NSString *)text {
    
    HUDView = [MBProgressHUD showHUDAddedTo:[self rootView] animated:YES];
    HUDView.mode = MBProgressHUDModeIndeterminate;
    HUDView.margin = 18.f;
    HUDView.opacity = 0.55;
    HUDView.dimBackground = YES;
    HUDView.labelText = text;
    HUDView.minShowTime = 0.3;
    HUDView.labelFont = [UIFont boldSystemFontOfSize:14];
}

+ (void)showWarningWithError:(id)error
{
    if(error == nil || [error isKindOfClass:[NSNull class]])
    {
        return;
    }
    if([error isKindOfClass:[NSString class]])
    {
        NSString * err = error;
        if(err.length == 0)
        {
            return;
        }
        [self showWarningWithText:err];
        return;
    }
    if([error isKindOfClass:[NSError class]])
    {
        NSError * err = error;
        if(err.code == -1004 || err.code == -999 || err.code == -1011 || err.code == -1001)
        {
            [self showWarningWithText:@"请检查网络"];
        }else
        {
            [self showWarningWithText:@"未知错误"];
        }
        return;
    }
}

+ (UIView*)rootView
{
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }
    return topController.view;
}


@end
